package www.sofn.com.sys.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.sofn.com.constant.Constants;
import www.sofn.com.domain.vo.PageRestVO;
import www.sofn.com.exception.ValidateException;
import www.sofn.com.sys.user.domain.pojo.SysUserPO;
import www.sofn.com.sys.user.domain.query.SysUserQuery;
import www.sofn.com.sys.user.domain.vo.ListSysUserVO;
import www.sofn.com.sys.user.domain.vo.SysUserVO;
import www.sofn.com.utils.PageUtils;
import www.sofn.com.utils.SecurityUtils;

import java.io.Serializable;
import java.util.Set;

@Service
public class SysUserService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private SysUserMapper sysUserMapper;

    public PageRestVO<ListSysUserVO> pageList(SysUserQuery query) {

        final Page<SysUserPO> page = sysUserMapper.selectPage(new Page(query.getCurrent(), query.getSize()),
                new LambdaQueryWrapper<SysUserPO>().like(StrUtil.isNotBlank(query.getName()),SysUserPO::getName, query.getName())
        );
        return PageUtils.copyPageVO(page,ListSysUserVO.class);

    }

    public SysUserPO selectUserByUsername(String username) {
        SysUserPO sysUserPO = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUserPO>().eq(SysUserPO::getUsername, username));
        return sysUserPO;
    }


    public void save(SysUserPO sysUserPO) {
        if (sysUserMapper.existUsername(sysUserPO.getUsername())) {
            throw new ValidateException("保存失败：该账号已存在，请刷新页面");
        }
        sysUserPO.setPassword(SecurityUtils.encryptPassword(sysUserPO.getPassword()));
        sysUserMapper.insert(sysUserPO);
    }

    public SysUserVO getById(String id) {
        return BeanUtil.toBean(sysUserMapper.selectById(id),SysUserVO.class);
    }

    @Transactional
    public void update(SysUserPO sysUserPO) {
        SysUserPO temp = sysUserMapper.selectById(sysUserPO.getId());
        if(ObjectUtil.isNull(temp))  return;
        sysUserMapper.updateById(sysUserPO);
        redisTemplate.delete(Constants.PERSONAL_CACHE_PREFIX + temp.getUsername());
    }

    public void deleteById(String id) {
        SysUserPO userPO = sysUserMapper.selectById(id);
        if (ObjectUtil.isNull(userPO)) return;
        sysUserMapper.deleteById(id);
        redisTemplate.delete(Constants.PERSONAL_CACHE_PREFIX + userPO.getUsername());
    }

    public Set<String> findResourceCodeByUserId(@NotNull Serializable id) {
        final Set<String> resourceCodeByUserId = sysUserMapper.findResourceCodeByUserId(id);
        return resourceCodeByUserId;
    }

    public Set<String> findRolesByUserId(@NotNull Serializable id){
        final Set<String> rolesIds = sysUserMapper.findRolesByUserId(id);
        return rolesIds;
    }
}
